from info import db
from info.modules.api import api_blu
from flask import jsonify, json, request
import subprocess

from info.utils.models import Input, Getoutput, Outputgroup

#from info.modules.api.neuralnet.shareglob import *

# from info.modules.api.neuralnet import craft_keras_out5

@api_blu.route('/api/input', methods=['GET', 'POST'])
def input():
    get_data = json.loads(request.get_data(as_text=True))
    Factory_name = get_data['Factory_name']
    Cotton_variety_count = get_data['Cotton_variety_count']
    Yarn_count = get_data['Yarn_count']
    Yarn_strength = get_data['Yarn_strength']
    Actual_twist = get_data['Actual_twist']
    Actual_twist_factor = get_data['Actual_twist_factor']
    Twisting_efficiency = get_data['Twisting_efficiency']
    Combed_combed = get_data['Combed_combed']
    Knitting_yarn = get_data['Knitting_yarn']
    Sirospun = get_data['Sirospun']
    Compact_spinning = get_data['Compact_spinning']
    # Key = get_data['Key']

    try:
        obj = Input(
            Factory_name=0,
            Cotton_variety_count=Cotton_variety_count,
            Yarn_count=Yarn_count,
            Yarn_strength=Yarn_strength,
            Actual_twist=Actual_twist,
            Actual_twist_factor=Actual_twist_factor,
            Twisting_efficiency=Twisting_efficiency,
            Combed_combed=Combed_combed,
            Knitting_yarn=0,
            Sirospun=0,
            Compact_spinning=0,
        )
        db.session.add(obj)
        db.session.commit()
        #        subprocess.call("mpiexec -n 11 python /home/hy/icds_new/info/modules/api/local_main_nosplit.py", shell=True)
#        print('textilenum= ', textilenum)

#        subprocess.call("mpiexec -n 16 python /home/hy/icds_new/info/modules/api/local_main_split.py", shell=True)
#        if (int(Cotton_variety_count) <10):
#           print('num <10')
#           subprocess.call("mpiexec -n 16 python /home/hy/icds_new/info/modules/api/local_main_split.py", shell=True)
#        else:
        if True:
#           print('num >9')
           subprocess.call("mpiexec -n 16 python /home/hy/icds_new/info/modules/api/test_main_split.py", shell=True)

    except Exception as e:
        print(e)

    result = {
        "error_code": 200,
        "reson": "success"
    }
    return jsonify(result)


@api_blu.route('/api/taicang/getoutput/', methods=['GET', 'POST'])
def cottonjs():
    OUT_PUT_GROUP = Outputgroup.query.order_by(Outputgroup.Group_id.desc()).limit(1).first()
    GET_OUT_PUT = Getoutput.query.filter_by(Group_id=OUT_PUT_GROUP.Group_id).all()
    DATA_INPUT = Input.query.order_by(Input.Id.desc()).limit(1).first()
    data = []
    for item in GET_OUT_PUT:
        data_json = {
            "Scale_factor": item.Scale_factor,
            "Batch_number": item.Batch_number,
            "Production_place": item.Production_place,
            "Micronaire": item.Micronaire,
            "Average_length": item.Average_length,
            "Fibre_strength": item.Fibre_strength,
            "Moisture_regain": item.Moisture_regain,
            "Trash_content": item.Trash_content,
            "Spinning_coefficient": item.Spinning_coefficient,
            "Maturity": item.Maturity,
            "Uniformity": item.Uniformity,
            "Fiber_index": item.Fiber_index,
            "Reflectivity": item.Reflectivity,
            "Yellowness_index": item.Yellowness_index,
            "Neps": item.Neps,
            "Package_id": item.Package_id,
        }
        data.append(data_json)

    jsonstring = {
        "User": OUT_PUT_GROUP.User,
        "Craft": OUT_PUT_GROUP.Craft,
        "Final_calculation_time": OUT_PUT_GROUP.Final_calculation_time,
        "data": data,
        "datalen": len(data),
        "datainput": DATA_INPUT.Cotton_variety_count,
    }

    return jsonify(jsonstring)


@api_blu.route('/neural_network')
def neural_network():
    try:
        from info.modules.api.neuralnet import craft_keras_out5
        #        subprocess.call("python /home/hy/icds_new/info/modules/api/neuralnet/craft_keras_out5.py", shell=True)
        craft_keras_out5.main()
    except Exception as e:
        print(e);
    result = {
        "error_code": 200,
        "reson": "success"
    }
    return jsonify(result)
